package com.upc.toss.fraction;

import com.upc.toss.ResourceTable;
import com.upc.toss.SettingProvider;
import com.upc.toss.slice.ChangeAbilitySlice;
import com.upc.toss.slice.LoginAbilitySlice;
import com.upc.toss.slice.MainAbilitySlice;
import com.upc.toss.util.Get;
import com.upc.toss.util.Update;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.app.Context;
import ohos.app.dispatcher.task.Revocable;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.prefs.BackingStoreException;

public class MyFraction extends Fraction {

    private static final String TAG = "MyFraction";

    Image imageChange;
    Text textExit;
    Text textId;
    String username;
    Preferences preferences;
    DatabaseHelper databaseHelper;

    private int MY_MODULE = 0x00203;
    private final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, MY_MODULE, TAG); //

    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container,
                                            Intent intent) {

        Component component = scatter.parse(ResourceTable.Layout_fraction_my, container, false);
        textId= (Text) component.findComponentById(ResourceTable.Id_text_id);

        return component;
    }

    protected void onStart(Intent intent) {

        super.onStart(intent);
        username = MainAbilitySlice.username;
        databaseHelper = new DatabaseHelper(LoginAbilitySlice.context); // context入参类型为ohos.app.Context。
        preferences = databaseHelper.getPreferences("user");// user表示文件名，其取值不能为空，也不能包含路径，默认存储目录可以通过context.getPreferencesDir()获取。

        imageChange = (Image) getFractionAbility().findComponentById(ResourceTable.Id_image_change);
        textExit = (Text) getFractionAbility().findComponentById(ResourceTable.Id_text_exit);
        textId.setText(username);

        imageChange.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent secondIntent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.upc.toss")
                        .withAbilityName(".ChangeAbility")
                        .build();
                secondIntent.setParam("name",username);
                secondIntent.setOperation(operation);
                getFractionAbility().startAbilityForResult(secondIntent,666);
            }
        });

        textExit.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {

                preferences.putString("username","");
                preferences.flush();
                Intent intent1= new Intent();
                 //通过Intent中的OperationBuilder类构造operation对象，指定设备标识（空串表示当前设备）、应用包名、Ability名称
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.upc.toss")
                        .withAbilityName("com.upc.toss.LoginAbility")
                        .build();
                intent1.setOperation(operation);
                getFractionAbility().startAbility(intent1);
                getFractionAbility().terminateAbility();
            }
        });
    }

    @Override
    protected void onActive() {
        super.onActive();
        System.out.println("onActive生命周期方法");
    }
    @Override
    protected void onInactive() {
        super.onInactive();
        System.out.println("onInactive生命周期方法");
    }
    @Override
    protected void onBackground() {
        super.onBackground();
        System.out.println("onBackground生命周期方法");
    }
    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
        System.out.println("onForeground生命周期方法");
    }
    @Override
    protected void onStop() {
        super.onStop();
        System.out.println("onStop生命周期方法");
    }

    @Override
    protected void onComponentDetach() {
        super.onComponentDetach();
        System.out.println("onComponentDetach生命周期方法");
    }
}
